package com.software.gateway.config;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

@Component
public class PreLogGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
    @Override
    public GatewayFilter apply(NameValueConfig config) {
        return ((exchange, chain) -> {
            String name = config.getName(); //name
            String value = config.getValue(); //value
            System.out.println("a:" + name);
            System.out.println("b:" + value);
            // 可以修改请求，把请求再传递至下一个过滤器,固定格式的代码，不能少！
            ServerHttpRequest request = exchange.getRequest().mutate().build();
            ServerWebExchange build = exchange.mutate().request(request).build();
            return chain.filter(build);
        });
    }
}
